import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;

public class AddMCQuestionGUI extends JPanel implements ActionListener, FocusListener{

	

/*

(BorderLayout CENTER)

Row1:	Type the question here

Row2:	How many possible responses?

Row3:	Response 1

Row4:	Response 2

Row5:	Response 3

Row6:	Response 4

Row7:	Response 5

(BorderLayout SOUTH)

Row8:	Cancel and Submit Buttons

*/

	

	//private JFrame mainFrame;

	private JPanel main, row1, row2, row3, row4, row5, row6, row7, row8, row9;

	private JButton cancelButton, submitButton;

	private JTextField question;//, response1, responses[1], responses[2], responses[3], responses[4];
	private JTextField [] responses;
	private JRadioButton radio1, radio2, radio3, radio4, radio5;

	private ButtonGroup buttonGroup;

	private JLabel	numberOfResponses, selectAResponse;

	private JComboBox comboBox;

	

	public AddMCQuestionGUI(){

		//super("Add a Multiple Choice Question");

		this.setLayout(new BorderLayout());

		//mainFrame = new JFrame("Add a Multiple Choice Question");

		main = new JPanel();

		main.setLayout(new BoxLayout(main,BoxLayout.Y_AXIS));

		row1 = new JPanel(new FlowLayout());

		row2 = new JPanel(new FlowLayout());

		row3 = new JPanel(new FlowLayout());

		row4 = new JPanel(new FlowLayout());

		row5 = new JPanel(new FlowLayout());

		row6 = new JPanel(new FlowLayout());

		row7 = new JPanel(new FlowLayout());

		row8 = new JPanel(new FlowLayout());

		

		question = new JTextField("Type your question here...");

		question.setPreferredSize(new Dimension(285,20));

		numberOfResponses = new JLabel("Select the number of possible responses: ");

		comboBox = new JComboBox(new String[] {"2","3","4","5"});

		comboBox.setSelectedIndex(3);

		buttonGroup = new ButtonGroup();

		radio1 = new JRadioButton();

		radio1.setFocusable(false);
		
		responses = new JTextField [5];
		responses[0] = new JTextField("Type the first response here...");
		responses[0].setPreferredSize(new Dimension(275,20));
		responses[0].selectAll();

		radio2 = new JRadioButton();

		radio2.setFocusable(false);

		responses[1] = new JTextField("Type the second response here...");
		responses[1].setPreferredSize(new Dimension(275,20));

		radio3 = new JRadioButton();

		radio3.setFocusable(false);

		responses[2] = new JTextField("Type the third response here...");
		responses[2].setPreferredSize(new Dimension(275,20));

		radio4 = new JRadioButton();

		radio4.setFocusable(false);

		responses[3] = new JTextField("Type the fourth response here...");
		responses[3].setPreferredSize(new Dimension(275,20));

		radio5 = new JRadioButton();

		radio5.setFocusable(false);

		responses[4] = new JTextField("Type the fifth response here...");
		responses[4].setPreferredSize(new Dimension(275,20));

		buttonGroup.add(radio1);

		buttonGroup.add(radio2);

		buttonGroup.add(radio3);

		buttonGroup.add(radio4);

		buttonGroup.add(radio5);

		cancelButton = new JButton("Cancel");

		submitButton = new JButton("Submit");

		

		row1.add(question);

		row2.add(numberOfResponses);

		row2.add(comboBox);

		row3.add(radio1);

		row3.add(responses[0]);

		row4.add(radio2);

		row4.add(responses[1]);

		row5.add(radio3);

		row5.add(responses[2]);

		row6.add(radio4);

		row6.add(responses[3]);

		row7.add(radio5);

		row7.add(responses[4]);

		row8.add(cancelButton);

		row8.add(submitButton);

		main.add(row1);

		main.add(row2);

		main.add(row3);

		main.add(row4);

		main.add(row5);

		main.add(row6);

		main.add(row7);

				

		// add action listeners to each of the buttons

		question.addFocusListener(this);

		comboBox.addActionListener(this);

		responses[0].addFocusListener(this);

		radio1.addActionListener(this);

		responses[1].addFocusListener(this);

		radio2.addActionListener(this);

		responses[2].addFocusListener(this);

		radio3.addActionListener(this);

		responses[3].addFocusListener(this);

		radio4.addActionListener(this);

		responses[4].addFocusListener(this);

		radio5.addActionListener(this);

		cancelButton.addActionListener(this);

	    submitButton.addActionListener(this);



	    this.add(main, BorderLayout.CENTER);

	    this.add(row8, BorderLayout.SOUTH);

		//mainFrame.add(main, BorderLayout.CENTER);

		//mainFrame.add(row8, BorderLayout.SOUTH);

		//mainFrame.setSize(500,500);

		//mainFrame.setVisible(true);

	}



	public void focusGained(FocusEvent e){

		if(e.getSource()==question){

			if(question.getText().equals("Type your question here...")){

				question.selectAll();

			}

		}else if(e.getSource()==responses[0]){

			if(responses[0].getText().equals("Type the first response here...")){

				responses[0].selectAll();

			}

		}else if(e.getSource()==responses[1]){

			if(responses[1].getText().equals("Type the second response here...")){

				responses[1].selectAll();

			}

		}else if(e.getSource()==responses[2]){

			if(responses[2].getText().equals("Type the third response here...")){

				responses[2].selectAll();

			}

		}else if(e.getSource()==responses[3]){

			if(responses[3].getText().equals("Type the fourth response here...")){

				responses[3].selectAll();

			}

		}else if(e.getSource()==responses[4]){

			if(responses[4].getText().equals("Type the fifth response here...")){

				responses[4].selectAll();

			}

		}

	}

	

	public void focusLost(FocusEvent e){}

	

	public void actionPerformed( ActionEvent event){

		GUIMaster gm = GUIMaster.getInstance();
		SystemController sc = gm.getWorkhorse();
		//Code for handling actions

		if(event.getSource()==comboBox){

			switch(comboBox.getSelectedIndex()){

			case 0:

				responses[2].setVisible(false);

				radio3.setVisible(false);

				responses[3].setVisible(false);

				radio4.setVisible(false);

				responses[4].setVisible(false);

				radio5.setVisible(false);

				break;

			case 1:

				responses[2].setVisible(true);

				radio3.setVisible(true);

				responses[3].setVisible(false);

				radio4.setVisible(false);

				responses[4].setVisible(false);

				radio5.setVisible(false);

				break;

			case 2:

				responses[2].setVisible(true);

				radio3.setVisible(true);

				responses[3].setVisible(true);

				radio4.setVisible(true);

				responses[4].setVisible(false);

				radio5.setVisible(false);

				break;

			case 3:

				responses[2].setVisible(true);

				radio3.setVisible(true);

				responses[3].setVisible(true);

				radio4.setVisible(true);

				responses[4].setVisible(true);

				radio5.setVisible(true);

				break;

			}

		}else if(event.getSource()==cancelButton){

			gm.questionAddCancelled();
			
			//Go to the last page--probably the page where a survey preview can be seen

			

		}else if(event.getSource()==submitButton){

			if(question.getText().equals("") || question.getText().equals("Type your question here...")){

				JOptionPane.showMessageDialog(this, "Please enter a question.", "Warning!", JOptionPane.WARNING_MESSAGE);
			}
			System.out.println("Index: " + (String)(comboBox.getSelectedItem()) );
			String answers [] = new String [Integer.parseInt((String)(comboBox.getSelectedItem()) )];
			int i;
			for(i = 0; i < answers.length; i++)
			{
				answers[i] = responses[i].getText();
			}
			
			Question q = new Question(question.getText(), 2, answers, sc.currentCid);
			//Database interaction--submit data to database
			sc.addSurveyQuestion(q); 
			gm.questionAddSuccessful();

		}

	}



	public static void main(String args[]){

		JFrame frame = new JFrame();

		frame.add(new AddMCQuestionGUI());

		//AddMCQuestionGUI application = new AddMCQuestionGUI();

        frame.setSize(500,500);

		frame.setVisible(true);

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

}